package animation;

import main.pak.R;
import android.app.Activity;
import android.content.ClipData;
import android.graphics.Typeface;
import android.os.Bundle;
import android.util.Log;
import android.view.DragEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.DragShadowBuilder;
import android.view.View.OnDragListener;
import android.view.View.OnTouchListener;
import android.widget.TextView;
import android.widget.Toast;

public class DragDropActivity extends Activity {

	// text views being dragged and dropped onto
	private TextView _option1, _option2, _option3, _choice1, _choice2,
			_choice3;
	private String TAG = "Drag and Drop";
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_drag_drop);

		// get both sets of text views
		// views to drag
		_option1 = (TextView) findViewById(R.id.option_1);
		_option2 = (TextView) findViewById(R.id.option_2);
		_option3 = (TextView) findViewById(R.id.option_3);

		// view to drop onto
		_choice1 = (TextView) findViewById(R.id.choice_1);
		_choice2 = (TextView) findViewById(R.id.choice_2);
		_choice3 = (TextView) findViewById(R.id.choice_3);

		// set touch listeners

		_option1.setOnTouchListener(new ChoiceTouchListener());
		_option2.setOnTouchListener(new ChoiceTouchListener());
		_option3.setOnTouchListener(new ChoiceTouchListener());

		// set drag listener
		_choice1.setOnDragListener(new ChoiceDragListener());
		_choice2.setOnDragListener(new ChoiceDragListener());
		_choice3.setOnDragListener(new ChoiceDragListener());
	}

	/**
	 * ChoiceTouchListener will handle touch events on draggable views
	 * 
	 */
	private final class ChoiceTouchListener implements OnTouchListener {

		@Override
		public boolean onTouch(View v, MotionEvent me) {
			if (me.getAction() == MotionEvent.ACTION_DOWN) {
				ClipData data = ClipData.newPlainText("", "");
				DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(v);
				v.startDrag(data, shadowBuilder, v, 0);

				return true;
			} else {
				return false;
			}
		}

	}

	/**
	 * DragListener will handle dragged views being dropped on the drop area -
	 * only the drop action will have processing added to it as we are not -
	 * amending the default behavior for other parts of the drag process
	 * 
	 */
	private final class ChoiceDragListener implements OnDragListener {

		@Override
		public boolean onDrag(View v, DragEvent event) {
			switch (event.getAction()) {
			case DragEvent.ACTION_DRAG_STARTED:
				Log.d(TAG, "ToanNM Start Drag");
				Toast.makeText(DragDropActivity.this, "ToanNM drag object", Toast.LENGTH_LONG).show();
				// no action necessary
				break;
			case DragEvent.ACTION_DRAG_ENTERED:
				// no action necessary
				break;
			case DragEvent.ACTION_DRAG_EXITED:
				// when user move drag object to outside of the bounding box of
				// view
				Log.d(TAG, "Drag outside view");
				// no action necessary
				break;
			case DragEvent.ACTION_DROP:
				// handle the dragged view being dropped over a drop view
				View view = (View) event.getLocalState();
				// stop displaying the view where it was before it was dragged
				view.setVisibility(View.INVISIBLE);
				// view dragged item is being dropped on
				TextView dropTarget = (TextView) v;
				// view being dragged and dropped
				TextView dropped = (TextView) view;
				// update the text in the target view to reflect the data being
				// dropped
				dropTarget.setText(dropped.getText());
				// make it bold to hightlight the fact that an item has been
				// dropped
				dropTarget.setTypeface(Typeface.DEFAULT_BOLD);
				// if an item has already been dropped here, there will be a tag
				Object tag = dropTarget.getTag();

				// if there is already an item here, set it back visible in its
				// original place
				if (tag != null) {
					// the tag is the view id already dropped here
					int existingID = (Integer) tag;
					// set original view visible again
					findViewById(existingID).setVisibility(View.VISIBLE);
				}
				// set the tag in the target view being dropped on - to the ID
				// of the view being dropped
				dropTarget.setTag(dropped.getId());
				break;
			case DragEvent.ACTION_DRAG_ENDED:
				Log.d(TAG, "Action Drag Ended");
				break;
			default:
				break;
			}

			return true;
		}

	}

}
